package cn.wolfcode.luowowo.mgrsite.web;

import cn.wolfcode.luowowo.article.domain.StrategyCatalog;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcoe.luowowo.common.util.AjaxResult;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("strategyDetail")
public class StrategyDetailController {

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IStrategyService strategyService;

    @Reference
    private IStrategyThemeService strategyThemeService;

    @Reference
    private IStrategyCatalogService strategyCatalogService;

    @Reference
    private IStrategyTagService strategyTagService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") StrategyDetailQuery qo) {
        //pageInfo
        model.addAttribute( "pageInfo", strategyDetailService.query( qo ) );
        return "strategyDetail/list";
    }

    @RequestMapping("/input")
    public String input(Model model, Long strategyId, Long id) {
        model.addAttribute( "strategies", strategyService.list() );
        model.addAttribute( "themes", strategyThemeService.list() );
        List <StrategyCatalog> strategyCatalogs = strategyCatalogService.queryCatalogByStrategyId( strategyId );
        if (id != null) {
            //攻略明细
            StrategyDetail strategyDetail = strategyDetailService.get( id );
            strategyDetail.setStrategyContent( strategyDetailService.queryContent( id ) );
            model.addAttribute( "strategyDetail", strategyDetail );
            //标签内容
            String tags = strategyTagService.selectByDetailId( id );
            model.addAttribute( "tags", tags );
        }

        return "strategyDetail/input";
    }

    @RequestMapping("/getCatalogByStrategyId")
    @ResponseBody
    public List <StrategyCatalog> getCatalogByStrategyId(Model model, Long strategyId) {
        List <StrategyCatalog> strategyCatalogs = strategyCatalogService.queryCatalogByStrategyId( strategyId );
        return strategyCatalogs;
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(StrategyDetail strategyDetail, String tags) {
        strategyDetailService.saveOrUpdate( strategyDetail, tags );
        return AjaxResult.SUCCESS;
    }

}
